﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ELMWCFServiceClass.ELMWCF;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Modularity;
using System.ComponentModel;
using ELMApplication.Common.Const;
using System.Linq;

namespace DataflowListModule.Views.NewDataflowView
{
    public class NewDataflowPrentationModel : INewDataflowPrentationModel
    {
        private ELMServiceClient _domaincontext;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager regionManager;
        private readonly IModuleManager moduleManager;

        public NewDataflowPrentationModel(INewDataflowView view, ELMServiceClient domaincontext, IEventAggregator eventAggregator, IRegionManager regionManager, IModuleManager moduleManager)
        {
            this._domaincontext = domaincontext;
            this._eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.moduleManager = moduleManager;

            this.View = view;
            InitialData();
            view.SetModel(this);
            
        }

        public void InitialData()
        {
            OutMessage = new Dataflow();
            OutMessage.SenderUser = Current.User;
            _domaincontext.GetMattersAsync();
            _domaincontext.GetMattersCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        OutMessage.AllMatter = data.Result;
                        OutMessage.Matter = OutMessage.AllMatter.First();
                        NotifyPropertyChanged("OutMessage");
                    }
                };
            _domaincontext.GetAllHumanAsync();
            _domaincontext.GetAllHumanCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        OutMessage.AllHuman = data.Result;
                        NotifyPropertyChanged("OutMessage");
                    }
                };
            GetMainTask();
            

        }

        public void GetMainTask()
        {
            _domaincontext = new ELMServiceClient();
            _domaincontext.GetMainTaskIdAsync(Current.User.HumanId);
            _domaincontext.GetMainTaskIdCompleted += (sender, data) =>
            {
                if (data.Error == null)
                {
                    OutMessage.TaskID = data.Result;
                    OutMessage.SubTaskId = data.Result;
                    OutMessage.CreateDate = DateTime.Now;
                    OutMessage.InfoClass = "1";
                    OutMessage.MaxIndex = 1;
                    OutMessage.StartDate = DateTime.Now;
                    NotifyPropertyChanged("OutMessage");
                }
            };
        }



        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INewDataflowPrentationModel Members

        public INewDataflowView View
        {
            get;
            set;
        }

        #endregion

        #region INewDataflowPrentationModel Members

        private Matter _itemMatter;
        public Matter ItemMatter
        {
            get
            {
                return _itemMatter;

            }
            set
            {
                _itemMatter = value;
                NotifyPropertyChanged("ItemMatter");
            }
        }
        private Human _sender;
        public Human Sender
        {
            get
            {
                return _sender;
            }
            set
            {
                _sender = value;
                NotifyPropertyChanged("Sender");
            }
        }

        private Human _receiver;
        public Human Receiver
        {
            get
            {
                return _receiver;
            }
            set
            {
                _receiver = value;
                NotifyPropertyChanged("Receiver");
            }
        }
        private Dataflow _dataflow;
        public Dataflow OutMessage
        {
            get
            {
                return _dataflow;
            }
            set
            {
                _dataflow = value;
                NotifyPropertyChanged("OutMessage");
            }
        }

      

        public int SendOutMessage()
        {
            int bl = 0;
            if ( OutMessage.Matter == null )
            {
                MessageBox.Show("请选择物料");

              
            }
            if (string.IsNullOrEmpty(OutMessage.SubTaskName))
            {
                MessageBox.Show("请填写标题");
            }
            else if (OutMessage.ReceiveUser == null)
            {
                MessageBox.Show("请选择人员");
            }
            else
            {
                _domaincontext = new ELMServiceClient();
                _domaincontext.SendNewDataAsync(OutMessage);
                _domaincontext.SendNewDataCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        bl = OutMessage.ID = data.Result;
                        NotifyPropertyChanged("OutMessage");
                        if(bl>0)
                        {
                            MessageBox.Show("发送成功");
                            GetMainTask();
                        }
                        else
                        {
                            MessageBox.Show("发送失败");
                        }
                    }
                };
            }
            return bl;
        }

        #endregion
    }
}
